package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.dao.entity.DevFertilizerChannel;
import cn.sciento.farm.devicemanager.dao.entity.DevWaterFertilizer;
import cn.sciento.farm.devicemanager.dao.mapper.DevWaterFertilizerMapper;
import cn.sciento.farm.devicemanager.service.FertilizerChannelService;
import cn.sciento.farm.devicemanager.service.WaterFertilizerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 水肥机管理 服务类
 * </p>
 *
 * @author wumu
 * @since 2019-05-14
 */
@Service
public class WaterFertilizerServiceImpl implements WaterFertilizerService {

    @Autowired
    private DevWaterFertilizerMapper waterFertilizerMapper;

    @Autowired
    private FertilizerChannelService fertilizerChannelService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insert(DevWaterFertilizer waterFertilizer) {
        // 判断水肥机eui是否重复
        int number = countEui(waterFertilizer.getEui());
        if(number >0){
            return ResultConstant.FERTILIZER_HAD.getCode();
        }
        int count = waterFertilizerMapper.insert(waterFertilizer);
        if(count > 0){
            count += createFertilizerChannel(waterFertilizer.getEui(),waterFertilizer.getChannelNumber());
            return count;
        }else {
            return ResultConstant.FAILED.getCode();
        }
    }

    private int createFertilizerChannel(String eui, Integer channelNumber) {
        int  count = 0;
        for(int i=0; i<= channelNumber;i++ ){
            DevFertilizerChannel channel = new DevFertilizerChannel();
            channel.setEui(eui);
            channel.setChannelNo(i);
            count += fertilizerChannelService.insert(channel);
        }
        return count;
    }

    private int countEui(String eui){
        QueryWrapper queryWrapper = new QueryWrapper<DevWaterFertilizer>();
        queryWrapper.eq("eui",eui);
        return waterFertilizerMapper.selectCount(queryWrapper);
    }

    @Override
    public int updateById(DevWaterFertilizer waterFertilizer) {
        return waterFertilizerMapper.updateById(waterFertilizer);
    }

    @Override
    public DevWaterFertilizer getById(Long id) {
        return waterFertilizerMapper.selectById(id);
    }

    @Override
    public DevWaterFertilizer getByEui(String eui) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("eui",eui);
        return waterFertilizerMapper.selectOne(queryWrapper);
    }

    @Override
    public int deleteById(Long id) {
        return waterFertilizerMapper.deleteById(id);
    }

    @Override
    public List<DevWaterFertilizer> getByAreaId(int areaId) {
        QueryWrapper queryWrapper = new QueryWrapper<DevWaterFertilizer>();
        queryWrapper.eq("area_id",areaId);
        List<DevWaterFertilizer> deviceWaterFertilizers = waterFertilizerMapper.selectList(queryWrapper);
        return deviceWaterFertilizers;
    }

    @Override
    public int updateOnline(String concentrator, int status) {
        return waterFertilizerMapper.updateOnline(concentrator,status);
    }

    @Override
    public int deleteByAreaId(Integer areaId) {
        QueryWrapper queryWrapper = new QueryWrapper<DevWaterFertilizer>();
        queryWrapper.eq("area_id",areaId);
        return waterFertilizerMapper.delete(queryWrapper);
    }
}
